package com.dd.demo.day3;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;


import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public enum MineDyEnum {

    DEMO(-1, "name", "const_", "unit");

    private Integer code;
    private String name;
    private String const_;
    private String unit;
    private static Map<Integer, MineDyEnum> enumMap = new HashMap<>();

    static {
        EnumSet<MineDyEnum> set = EnumSet.allOf(MineDyEnum.class);
        for (MineDyEnum each : set) {
            enumMap.put(each.code, each);
        }
    }

    MineDyEnum(Integer code, String name, String const_, String unit) {
        this.code = code;
        this.name = name;
        this.const_ = const_;
        this.unit = unit;
    }


    public String getName() {
        return name;
    }

    public String getConst_() {
        return const_;
    }

    public String getUnit() {
        return unit;
    }

    public Integer getCode() {
        return code;
    }

    public MineDyEnum getEnum(Integer value) {
        return enumMap.get(value);
    }

    public MineDyEnum addEnum(String enumName, Integer code, String name, String const_, String unit) {
        MineDyEnum mineDyEnum = DynamicEnumUtil.addEnum(MineDyEnum.class, enumName, new Class[]{Integer.class, String.class, String.class, String.class}, new Object[]{code, name, const_, unit});
        enumMap.put(code, mineDyEnum);
        return mineDyEnum;
    }

    public static void main1(String[] args) {
        /**
         *    {
         *  *     code: 0,
         *  *     name: '温度',
         *  *     const: 'TEMP',
         *  *     unit: '℃'
         *  *   },
         *  *   {
         *  *     code: 1,
         *  *     name: '湿度',
         *  *     const: 'HUMI',
         *  *     unit: '%'
         *  *   },
         *
         *
         * 参数1. 枚举值名称 无实际意义 只要不重复即可。甚至可以使用uuid
         * 参数2  code 您数据给出的code 0 1
         * 参数3 name  气压  湿度
         * 参数4 const 常数 （TEMP,HUMI）
         * 参数5 unit 单位（℃，%）
         */
        //1.动态新增枚举
        MineDyEnum hahh = MineDyEnum.DEMO.addEnum("qiya", 2, "气压", "MPA", "hPa");
        //2.通过code 获取枚举
        MineDyEnum anEnum = hahh.getEnum(2);
        //3.打印枚举的信息
        System.out.println("枚举名称：code:[" + anEnum.getCode() + "] , 枚举name：[" + anEnum.getName() + "] , 枚举const_:[" + anEnum.getConst_() + "] , 枚举unit:[" + anEnum.getUnit() + "] ");
    }

    public static void main(String[] args) {
        String data = "[\n" +
                "   {\n" +
                "     code: 0,\n" +
                "     name: '温度',\n" +
                "     const: 'TEMP',\n" +
                "     unit: '℃'\n" +
                "   },\n" +
                "   {\n" +
                "     code: 1,\n" +
                "     name: '湿度',\n" +
                "     const: 'HUMI',\n" +
                "     unit: '%'\n" +
                "   },\n" +
                "   {\n" +
                "     code: 2,\n" +
                "     name: '气压',\n" +
                "     const: 'MPA',\n" +
                "     unit: 'hPa'\n" +
                "   },\n" +
                "   {\n" +
                "     code: 3,\n" +
                "     name: '风向',\n" +
                "     const: 'WD',\n" +
                "     unit: '°'\n" +
                "   },\n" +
                "   {\n" +
                "     code: 4,\n" +
                "     name: '风速',\n" +
                "     const: 'WS',\n" +
                "     unit: 'm/s'\n" +
                "   },\n" +
                "   {\n" +
                "     code: 5,\n" +
                "     name: '一氧化碳',\n" +
                "     const: 'CO',\n" +
                "     unit: 'mg/m³'\n" +
                "   },\n" +
                "   {\n" +
                "     code: 6,\n" +
                "     name: '二氧化氮',\n" +
                "     const: 'NO2',\n" +
                "     unit: 'μg/m³'\n" +
                "   },\n" +
                "   {\n" +
                "     code: 7,\n" +
                "     name: '臭氧',\n" +
                "     const: 'O3',\n" +
                "     unit: 'μg/m³'\n" +
                "   },\n" +
                "   {\n" +
                "     code: 8,\n" +
                "     name: '二氧化硫',\n" +
                "     const: 'SO2',\n" +
                "     unit: 'μg/m³'\n" +
                "   },\n" +
                "   {\n" +
                "     code: 9,\n" +
                "     name: 'pm10',\n" +
                "     const: 'PM10',\n" +
                "     unit: 'μg/m³'\n" +
                "   },\n" +
                "   {\n" +
                "     code: 10,\n" +
                "     name: 'pm2.5',\n" +
                "     const: 'PM25',\n" +
                "     unit: 'μg/m³'\n" +
                "   }\n" +
                " ]";

        JSONArray objects = JSONUtil.parseArray(data);
        if (null != objects && objects.size() > 0) {
            for (int i = 0; i < objects.size(); i++) {
                JSONObject jsonObject = objects.getJSONObject(i);
                MineDyEnum.DEMO.addEnum(UUID.randomUUID().toString(), jsonObject.getInt("code"), jsonObject.getStr("name"), jsonObject.getStr("const"), jsonObject.getStr("unit"));
            }
        }
        Map<Integer, MineDyEnum> enumMap1 = MineDyEnum.enumMap;
        if (null != enumMap1 && enumMap1.size() > 0) {
            for (Map.Entry<Integer, MineDyEnum> entry : enumMap1.entrySet()) {
                MineDyEnum anEnum = entry.getValue();
                System.out.println("code:" + anEnum.getCode() + " , name：\'" + anEnum.getName() + " \', const:\'" + anEnum.getConst_() + " \', unit:\'" + anEnum.getUnit() + "\' ");
            }
        }

        System.out.println("....");

//        MineDyEnum hahh = MineDyEnum.DEMO.addEnum( "qiya",2,"气压", "MPA","hPa");
//        //2.通过code 获取枚举
//        MineDyEnum anEnum = hahh.getEnum(2);
//        //3.打印枚举的信息
//        System.out.println("枚举名称：code:[" + anEnum.getCode() + "] , 枚举name：["  + anEnum.getName()+"] , 枚举const_:["+anEnum.getConst_()+"] , 枚举unit:[" + anEnum.getUnit()+"] ");
//        System.out.println("....");
    }

}
